package com.choosefine.paycenter.webgateway.account.web.controller;

import com.choosefine.paycenter.account.model.AccountBillDetail;
import com.choosefine.paycenter.account.vo.BizzOrderVo;
import com.choosefine.paycenter.common.dto.JSONResult;
import com.choosefine.paycenter.common.enums.AccountBillStatus;
import com.choosefine.paycenter.common.enums.PayStatus;
import com.choosefine.paycenter.common.enums.TradeType;
import com.choosefine.paycenter.common.utils.JSONResultGenerator;
import com.choosefine.paycenter.account.dto.AccountBillDto;
import com.choosefine.paycenter.account.dto.ReceivePaymentDto;
import com.choosefine.paycenter.account.model.AccountBill;
import com.choosefine.paycenter.account.service.AccountBillService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 账单
 * Created by Jay Chang on 2017/3/7.
 */
@RestController
@RequestMapping("accountBill")
@Api(value = "账单流水", description = "邓友谊")
public class AccountBillController {

    @Autowired
    private AccountBillService accountBillService;

    /**
     * 根据accountId分页条件查询账单流水
     * @param accountBillDto
     * @return
     */
    @ApiOperation("分页条件查询某个账户账单流水")
    @GetMapping( "/getBillPageByAccountId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "header", dataType = "String", required = true, value = "交易主体账户租户编码"),
            @ApiImplicitParam(name = "tradeType", paramType = "query",allowableValues = "TRANSFER,WITHDRAW,RECHARGE,PAY_AGENT,SALARY,SHOPPING,SUBPACKAGE,REFUND",dataType = "String", required = false, value = "交易类型"),
            @ApiImplicitParam(name = "tradeMemo", paramType = "query", dataType = "String", required = false, value = "交易备注"),
            @ApiImplicitParam(name = "beforeMonth", paramType = "query", dataType = "String", required = false, value = "距离当前月份"),
            @ApiImplicitParam(name = "fundFlow", paramType = "query", allowableValues = "IN,OUT",dataType = "String", required = false, value = "资金流向:IN(流入)，OUT(支出)"),
            @ApiImplicitParam(name = "status", paramType = "query", allowableValues = "WAIT,COMPLETE,CLOSED,FAILURE",dataType = "String", required = false, value = "账单状态：交易状态：WAIT(等待)，COMPLETE(完成)，CLOSED(关闭),FAILURE(失败))"),
            @ApiImplicitParam(name = "startTime", paramType = "query", dataType = "Long", required = false, value = "开始时间"),
            @ApiImplicitParam(name = "endTime", paramType = "query", dataType = "Long", required = false, value = "截止时间"),
            @ApiImplicitParam(name = "pageNum", dataType = "Long", paramType = "query", required = false, value = "页码",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", dataType = "Long", paramType = "query", required = false, value = "每页显示",defaultValue = "10"),
            @ApiImplicitParam(name = "accountBillDto", dataType = "String", paramType = "query", required = false, value = "自动生成不填写"),
    })
    public JSONResult getBillPageByAccountId(@RequestHeader("userCode") @NotBlank String userCode ,AccountBillDto accountBillDto){
        accountBillDto.setMainAccountUserCode(userCode);
        PageInfo<AccountBill> page = accountBillService.findPageByAccountId(accountBillDto);
        return JSONResultGenerator.buildQuerySuccessResult(page);
    }

    @ApiOperation("分页条件查询某个账户收款明细")
    @GetMapping( "/getReceivePaymentList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "query", dataType = "String", required = true, value = "交易主体账户租户编码"),
            @ApiImplicitParam(name = "tradeType", paramType = "query",allowableValues = "TRANSFER,WITHDRAW,RECHARGE,PAY_AGENT,SALARY,SHOPPING,SUBPACKAGE,REFUND",dataType = "String", required = false, value = "交易类型"),
            @ApiImplicitParam(name = "fundFlow", paramType = "query", allowableValues = "IN,OUT",dataType = "String", required = false, value = "资金流向:IN(流入)，OUT(支出)"),
            @ApiImplicitParam(name = "receivePaymentDto", dataType = "String", paramType = "query", required = false, value = "自动生成不填写"),
    })
    public JSONResult getReceivePaymentList(@Validated({AccountBillService.findReceivedPayment.class})  ReceivePaymentDto receivePaymentDto){
        PageInfo<ReceivePaymentDto> page = accountBillService.findReceivedPayment(receivePaymentDto);
        return JSONResultGenerator.buildQuerySuccessResult(page);
    }

    @ApiOperation(value = "获取所有交易类型列表")
    @RequestMapping(value = "/tradeType", method = RequestMethod.GET)
    public JSONResult getAllTradeType() {
        return JSONResultGenerator.buildQuerySuccessResult(TradeType.getLookUp());
    }


    @ApiOperation(value = "根据不同角色获取交易类型列表")
    @RequestMapping(value = "/tradeType/{roleId}", method = RequestMethod.GET)
    public JSONResult getAllTradeTypeByRoleId(@PathVariable("roleId")Long roleId) {
        Map<String,String> tradeTypeMap = accountBillService.findAllTradeTypeByRoleId(roleId);
        return JSONResultGenerator.buildQuerySuccessResult(tradeTypeMap);
    }

    @ApiOperation(value = "获取交易状态列表")
    @RequestMapping(value = "/tradeStatus", method = RequestMethod.GET)
    public JSONResult getAllTradeStatus() {
        return JSONResultGenerator.buildQuerySuccessResult(AccountBillStatus.getLookUp());
    }

    @ApiOperation("关闭交易账单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "header", dataType = "String", required = true, value = "租户编码"),
            @ApiImplicitParam(name = "accountBillId", paramType = "query", dataType = "Long", required = true, value = "账单id")
    })
    @RequestMapping(value="/close",method = RequestMethod.PUT)
    public JSONResult closeAccountBill(@RequestHeader("userCode") String userCode,@RequestParam("accountBillId") Long accountBillId){
        accountBillService.closeAccountBill(userCode,accountBillId);
        return JSONResultGenerator.buildOperSuccessResult();
    }


    @ApiOperation("根据账单id查询业务单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "header", dataType = "String", required = true, value = "租户编码")
    })
    @RequestMapping(value="/bizzOrder",method = RequestMethod.GET)
    public JSONResult findBizzSysAndBizzSn(@RequestHeader("userCode") String userCode,@RequestParam("accountBillId") Long accountBillId){
        BizzOrderVo bizzOrderVo = accountBillService.findBizzOrderByAccountId(accountBillId);
        return JSONResultGenerator.buildQuerySuccessResult(bizzOrderVo);
    }

    @ApiOperation("根据账单id查询账单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", paramType = "header", dataType = "String", required = true, value = "租户编码")
    })
    @RequestMapping(value="/accountBillDetail",method = RequestMethod.GET)
    public JSONResult accountBillDetail(@RequestHeader("userCode") String userCode,@RequestParam("accountBillId") Long accountBillId){
        AccountBillDetail detail = accountBillService.findAccountBillDetailById(accountBillId);
        return JSONResultGenerator.buildQuerySuccessResult(detail);
    }


}
